#!/usr/bin/ruby
$: << File.join(File.expand_path(File.dirname(__FILE__)), "..")

require 'rubygems'
require 'optparse'
require 'EasyHadoop'

#==================================
# ColExtractor.rb
# Extract particular columns from the datasets
#==================================
class ColSplit < EasyHadoop
	
	def initialize(arguments)
		super(arguments)
	end
	
	def set_mandatory
		super
		@mandatory << :col
		@mandatory << :col_delimiter
		@mandatory << :limit
	end
	
	def set_commands
		super
		@optparse.banner = "Usage: ruby #{File.basename(__FILE__)} --key COMMA_LIST [OPTIONS]"
		
		@options[:col] = nil
		@optparse.on('-c','--column=NUM', Integer, 'Column index number to be split') do |idx|
			@options[:col] = idx
		end

		@options[:col_delimiter]='|'
		@optparse.on('--column-delimiter=DELM', 'Separator between column values') do |sep|
			@options[:col_delimiter]=OptionParserUtil.handle_delimiter(sep, '|') 
		end
		
		@options[:limit]=nil
		@optparse.on('-l','--limit=NUM', Integer, 'Number of top N values to retain') do |l|
			@options[:limit] = l
		end

	end
	
	
	def execute(line)
		
		return line if line.nil? or line.chomp.empty?
		
		tokens = line.chomp.split(@options[:input_delimiter])
		
		#Sanity Check
		if tokens.length-1 < @options[:col]
			return line if @options[:ignore]
			raise Exception, "Expecting atleast #{@max_index+1} columns. Found #{tokens.length}"
		end
		
		#extract values
		values = tokens[@options[:col]].split(@options[:col_delimiter])
		values = [] if values.nil? 
		values = (values.length < @options[:limit]) \
					? values.fill(" ", values.length, @options[:limit]-values.length) \
					: values[0..@options[:limit]-1]
		 
		tokens = tokens + values
		return tokens.join(@options[:output_delimiter])
	end
end

# Scripting Option
if __FILE__ == $0
	cls = ColSplit.new(ARGV)
    STDIN.each_line{|line|
	  value = cls.execute(line)
	  if value
	  	puts value
	  else
	  	puts line.chomp
	  end
    }
end
